package com.ophiux.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ophiux.common.core.base.Result;
import com.ophiux.common.core.enums.ResultEnum;
import com.ophiux.common.core.exception.APIException;
import com.ophiux.member.mapper.MemberMapper;
import com.ophiux.member.model.dto.MemberDTO;
import com.ophiux.member.model.entity.Member;
import com.ophiux.member.model.vo.MemberAddVo;
import com.ophiux.member.model.vo.MemberLoginReqVo;
import com.ophiux.member.model.vo.MemberQueryVo;
import com.ophiux.member.model.vo.MemberUpdateVo;
import com.ophiux.member.service.IMemberService;
import io.seata.core.context.RootContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;

@Service
@Slf4j
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {

    @Value("${server.port}")
    public String port;

    @Override
    public String getUserById(Integer userId) {
        System.out.println("userId = " + userId);
        return "hhl-man-vip会员1-端口:" + port;
    }

    @Override
    public IPage<MemberDTO> getPage(IPage<Member> iPage, MemberQueryVo queryDTO) {
        LambdaQueryWrapper<Member> lambda = new LambdaQueryWrapper<Member>();
        return null;
    }


    @Override
    public Result insert(MemberAddVo dto) {
        Member member = new Member();
        BeanUtils.copyProperties(dto, member);
        member.setState();
        member.setRegisterTime(LocalDateTime.now());
        int effCount = this.baseMapper.insert(member);
        if (effCount > 0) return Result.success();
        return Result.fail();
    }

    @Override
    public Result update(MemberUpdateVo vo) {
        Member data = this.getById(vo.getId());
        if (data == null) return Result.fail(ResultEnum.DATA_NOT_FOUND);
        BeanUtils.copyProperties(vo, data);

        boolean isSuccess = this.updateById(data);
        if (!isSuccess) return Result.fail(ResultEnum.DATA_UPDATE_FAIL);

        return Result.success(ResultEnum.requestSuccess);
    }

    @Override
    public Result<MemberDTO> login(MemberLoginReqVo vo) {
        if (ObjectUtils.isEmpty(vo.getOpenId())) return Result.fail("微信授权失败");

        LambdaQueryWrapper<Member> lambdaQueryWrapper = new LambdaQueryWrapper<Member>();
        lambdaQueryWrapper.eq(Member::getOpenId, vo.getOpenId());
        Member member = getOne(lambdaQueryWrapper);
        synchronized (this) {
            if (member == null) {//会员不存在，新增会员
                BeanUtils.copyProperties(vo, member);
                boolean isOk = save(member);
                if (!isOk) return Result.fail("登录失败");
            }
        }
        MemberDTO memberDto = new MemberDTO();
        BeanUtils.copyProperties(member, memberDto);
        return Result.success(memberDto);
    }

    @Override
    public Result updateAmount(Integer amount) {
        log.info("member begin ... xid: " + RootContext.getXID());
        log.info("修改用户金额中，金额：{}", amount);
        int i = this.baseMapper.updateAmount(1,amount);
        if (i < 1) {
            throw new APIException("余额不足","NOT MONEY");
        }

        log.info("修改用户金额成功。。。。。。");
        return Result.success();
    }
}